/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PowerUp : FlyingObject
    {
        protected const short WIDTH = 32;
        protected const short HEIGHT = 32;
        protected const byte XYSpeed = 1;
        protected TimeSpan _previousUpdate;
        public PowerUp(Game game, Texture2D texture)
            : base(game)
        {
            // TODO: Construct any child components here
            _power = 50;
            
            _texture = texture;
            _position = new Vector2();
            //_position.X = Util.Instance.RandomGenerator.Next(Util.ScreenWidth);
            _spriteRectangle = new Rectangle(235, 235, WIDTH, HEIGHT);
            _spriteBatch = 
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            _speed = new Vector2();
            //InitSpeed();
        }

        private void InitSpeed()
        {
            int sign = (Util.Instance.RandomGenerator.Next(0, 2) > 0) ? 1 : -1;
            _speed.X = (Util.Instance.RandomGenerator.Next(XYSpeed) + 1) * sign;
            _speed.Y = Util.Instance.RandomGenerator.Next(XYSpeed) + 1;
          
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            _position.X = Util.Instance.RandomGenerator.Next(Util.ScreenWidth);
            _position.Y = 0;
            InitSpeed();
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            TimeSpan duration=gameTime.TotalGameTime.Subtract(_previousUpdate);
            if (duration.CompareTo(new TimeSpan(0, 0, 5)) > 0)//if more than 1 sec since previous update
            {
                InitSpeed();
                _previousUpdate = gameTime.TotalGameTime;
            }
            Move();
            
            base.Update(gameTime);            
        }

       

        private void Move()
        {
            _position.X += _speed.X;
            _position.Y += _speed.Y;
            if (_position.X > Util.ScreenWidth-WIDTH)
                _speed.X = -1 * Math.Abs(_speed.X);
            if (_position.X < 0)
                _speed.X = Math.Abs(_speed.X);
          
        }
        public void stay()
        {
            _position.X = -200;
            _position.Y = -200;
            _speed.X = 0;
            _speed.Y = 0;
        }
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, _position, _spriteRectangle, Color.White);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
        public void PutInPosition(int x , int y)
        {
            _position.X = x;
            _position.Y = y;
        }

        public bool CheckCollisionRect(Rectangle rect)
        {
            Rectangle spriterect = new Rectangle((int)_position.X, (int)_position.Y, WIDTH, HEIGHT);
            return spriterect.Intersects(rect);

        }


        public Rectangle BoundsRect()
        {
            return new Rectangle((int)_position.X, (int)_position.Y, (int)WIDTH, (int)HEIGHT);
        }
    }
}